package com.assignwork.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.SneakyThrows;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.DateUtil;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.lang.reflect.Method;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springblade.core.tool.utils.Func;
import org.springframework.format.annotation.DateTimeFormat;

/**
 * (TrainStatistics)VO
 * @since 2024-04-25 14:09:44
 */
@Data
@ApiModel(value = "TrainStatisticsVO", description = "")
public class TrainStatisticsVO  {

    @JsonSerialize(using = ToStringSerializer.class)
    private Long id;

    private String remark;

    @DateTimeFormat(pattern = DateUtil.PATTERN_DATE)
    @JsonFormat(pattern = DateUtil.PATTERN_DATE)
    private LocalDate planDate;

    private String planner;

    private String szysc;

    private String qc;

    private String pc;

    private String hwzc;

    private String sjzc;

    private String tqzc;

    private String syzc;

    private String zyxc;

    private List<TrainStatisticsItemVO> data;

    public String getCargoTypeCode() {
        if (Func.isNotEmpty(data)) {
            return data.stream().map(TrainStatisticsItemVO::getCargoType).distinct().collect(Collectors.joining(","));
        }
        return "";
    }

    public Kv getFormData() {
        List<String> keyList = Func.toStrList("SZYSC,QC,PC,HWZC,SJZC,TQZC,SYZC,ZYXC");
        if (Func.isNotEmpty(data)) {
            Map<String, List<TrainStatisticsItemVO>> wn = data.stream().collect(Collectors.groupingBy(TrainStatisticsItemVO::getWorkNode));
            Kv res = Kv.create();
            for (Map.Entry<String, List<TrainStatisticsItemVO>> wnEntry : wn.entrySet()) {
                String key = wnEntry.getKey();
                List<TrainStatisticsItemVO> value = wnEntry.getValue();
                Map<String, List<TrainStatisticsItemVO>> cc = value.stream().collect(Collectors.groupingBy(TrainStatisticsItemVO::getCargoCategory));
                Kv ccKv = Kv.create();
                for (Map.Entry<String, List<TrainStatisticsItemVO>> ccEntry : cc.entrySet()) {
                    String key1 = ccEntry.getKey();
                    List<TrainStatisticsItemVO> value1 = ccEntry.getValue();
                    List<Kv> items = new ArrayList<>();
                    for (TrainStatisticsItemVO itemVo : value1) {
                        Kv itemKv = Kv.create();
                        itemKv.put("cargoType", itemVo.getCargoType());
                        itemKv.put("cargoTypeName", itemVo.getCargoTypeName());
                        itemKv.put("amount", itemVo.getAmount());

                        items.add(itemKv);
                    }

                    ccKv.put(key1, items);
                }
                res.put(key, ccKv);
            }

            for (String s : keyList) {
                String remark1 = this.getRemark(s);
                if (Func.isNotEmpty(remark1)) {
                    if (!res.containsKey(s)) {
                        res.put(s, Kv.create());
                    }
                    Kv kv = (Kv) res.get(s);
                    kv.put("remark", remark1);
                }
            }
            res.put("id", getId() + "");
            res.put("remark", this.getRemark("remark"));
            return res;
        }

        return null;
    }

    @SneakyThrows
    private String getRemark(String key) {
        String s = Func.firstCharToUpper(key.toLowerCase());
        Method declaredMethod = TrainStatisticsVO.class.getDeclaredMethod("get" + s);
        Object invoke = declaredMethod.invoke(this);
        return Func.toStr(invoke);
    }

}

